package com.gec.wiki.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.wiki.po.Ebook;
import com.gec.wiki.po.req.EbookQueryReq;
import com.gec.wiki.po.req.EbookSaveReq;
import com.gec.wiki.po.resp.CommonResp;
import com.gec.wiki.po.resp.EbookQueryResp;
import com.gec.wiki.po.resp.PageResp;
import com.gec.wiki.service.IEbookService;
import com.gec.wiki.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 电子书 前端控制器
 * </p>
 *
 * @author 林云
 * @since 2023-10-30
 */
@RestController
@RequestMapping("/ebook")
public class EbookController {

    @Resource
    private IEbookService ebookService;

    @GetMapping("/getEbookByName")
    public CommonResp<List<EbookQueryResp>> getEbookByName(String name) {
        List<EbookQueryResp> ebookRespList = ebookService.getEbookByName(name);
        return CommonResp.success(ebookRespList);
    }

    @GetMapping("/getEbookListByPage")
    public CommonResp getEbookListByPage(@Valid EbookQueryReq ebookReq) {
        Page<Ebook> page = new Page<>(ebookReq.getPage(), ebookReq.getSize());
        page = ebookService.page(page);

        List<Ebook> ebooks = page.getRecords();

        List<EbookQueryResp> ebookReqs = CopyUtil.copyList(ebooks, EbookQueryResp.class);  // 转为专门封装ebook响应数据的类型

        PageResp<EbookQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(page.getTotal());
        pageResp.setList(ebookReqs);

        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp();  // 从原来的只携带分页数据-> 除了数据 还有总页数
        resp.setContent(pageResp);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@RequestBody EbookSaveReq req) {
        Ebook ebook = CopyUtil.copy(req, Ebook.class);
        //会根据是否有id判断   修改（有id）  新增（无id）
        ebookService.saveOrUpdate(ebook);
        CommonResp resp = new CommonResp<>();
        return resp;
    }

    @GetMapping("/remove")
    public CommonResp remove(Long id) {
        ebookService.removeById(id);
        return CommonResp.success();
    }

    @PostMapping("/uploadImage")
    public CommonResp uploadImage(@RequestParam(value = "file", required = false) MultipartFile file) {
        //动态获取项目所在的根目录
        String path = System.getProperty("user.dir");
        System.out.println(path);
        // 根目录的上一级
        String path2 = path.substring(0, path.lastIndexOf("\\"));
        System.out.println("path ============== " + path2);
        String imageName = ebookService.uploadImage(file, path2 + "\\wiki_vue\\public\\image\\"); //第二个参数指定成前端项目的路径
        CommonResp<Object> resp = new CommonResp<>();
        resp.setContent(imageName);
        return resp;
    }

    @GetMapping("/getEbooksByPage")
    public CommonResp getEbooksByPage(@Valid EbookQueryReq ebookQueryReq){
        System.out.println(ebookQueryReq.getCategory2Id());
        PageResp<EbookQueryResp> ebookListByPage = ebookService.getEbookListByPage(ebookQueryReq);

        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();  // 从原来的只携带分页数据-> 除了数据 还有总页数
        resp.setContent(ebookListByPage);
        return resp;
    }

}
